package com.lpc.carinfo.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lpc.carinfo.MainActivity;
import com.lpc.carinfo.R;
import com.lpc.carinfo.bean.CarInfoBean;
import com.lpc.carinfo.db.MyDB;

import java.util.List;

import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;

/**
 * Created by lipc0113 on 2017/11/18.
 */

public class CarInfoAdapter extends BaseAdapter {

    private Context context;
    private List<CarInfoBean> lists;

    public CarInfoAdapter(Context context, List<CarInfoBean> lists) {

        this.context = context;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int i) {
        return lists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int index, View view, ViewGroup viewGroup) {

        CarInfoBean bean = (CarInfoBean) getItem(index);

        Holder holder = null;

        if (view == null) {
            holder = new Holder();
            view = View.inflate(context, R.layout.car_info_adapter, null);
            holder.carSeries = view.findViewById(R.id.carSeries);
            holder.engineOil = view.findViewById(R.id.engineOil);
            holder.jiLv = view.findViewById(R.id.jiLv);
            holder.kongLv = view.findViewById(R.id.kongLv);
            holder.kongTiaoLv = view.findViewById(R.id.kongTiaoLv);
            holder.qilv = view.findViewById(R.id.qilv);
            holder.huosaihua = view.findViewById(R.id.huosaihua);
            holder.ll_carSeries = view.findViewById(R.id.ll_carSeries);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        holder.carSeries.setText(bean.getCarSeries());
        holder.engineOil.setText(bean.getEngineOil());
        holder.jiLv.setText(matcherSearchText(bean.getJiLv()));
        holder.kongLv.setText(matcherSearchText(bean.getKongLv()));
        holder.kongTiaoLv.setText(matcherSearchText(bean.getKongTiaoLv()));
        holder.qilv.setText(matcherSearchText(bean.getQilv()));
        holder.huosaihua.setText(matcherSearchText(bean.getHuosaihua()));

        holder.ll_carSeries.setTag(index);
        holder.ll_carSeries.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
                final int tag = (int) view.getTag();

                dialog.setTitle("提示")
                        .setMessage("确定删除该条目吗？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                MyDB.getInstace().delete(((CarInfoBean)getItem(tag)).getCarSeries());
                            }
                        })
                        .setNegativeButton("否", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setCancelable(true)
                        .show();
                return false;
            }
        });

        return view;
    }

    class Holder {

        TextView carSeries;
        TextView engineOil;
        TextView jiLv;
        TextView kongLv;
        TextView kongTiaoLv;
        TextView qilv;
        TextView huosaihua;
        LinearLayout ll_carSeries;
    }

    public CharSequence matcherSearchText(String string) {
        SpannableStringBuilder builder = new SpannableStringBuilder(string);
        int indexOf = string.indexOf("/");
        if (indexOf != -1) {
            builder.setSpan(new ForegroundColorSpan(Color.parseColor("#DC143C")), indexOf + 1, string.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }
}
